package com.qjcc.util;

import android.content.Context;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import com.qjcc.R;

public class SessionUtil {
	//同步cookie
	public static void synCookies(Context context, String url,String cookies) {
		CookieSyncManager.createInstance(context);
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.setAcceptCookie(true);  
		cookieManager.removeSessionCookie();//移除  
		cookieManager.setCookie(url, cookies);//cookies是在HttpClient中获得的cookie  
		CookieSyncManager.getInstance().sync();
	}

	/**
	 * 同步session
	 * @param context
	 */
	public static void synSession(Context context) {
		String appUrl = context.getApplicationContext().getString(R.string.serverUrl);
		String domain = context.getApplicationContext().getString(R.string.domain);
		String sessionId = SharedPreferencesUtil.getSessionId(context);
		String cookie = "";
		if(sessionId != null){
			cookie = sessionId + "; domain="+domain+"; Path=/dms/; HttpOnly; ";
			SessionUtil.synCookies(context, appUrl, cookie);
		}
	}
}
